<template>
  <div class="data-view">
    <el-card>
      <div id="main1"></div>
    </el-card>
    <el-card>
      <div id="main2"></div>
    </el-card>
  </div>
</template>

<script>
import { dataview } from '../../Api/api.js'
export default {
  data() {
    return {}
  },
  created() {
    dataview()
      .then((res) => {
        if (res.data.status == 200) {
          // 将三个值进行解构出来赋值
          let { legend, xAxis, series } = res.data.data
          this.draw(legend, xAxis, series)
        }
      })
      .catch((err) => {
        throw err
      })
  },
  mounted() {
    var myChart = this.$echarts.init(document.getElementById('main1'))
    myChart.setOption({
      title: {
        text: 'ECharts 入门示例',
      },
      tooltip: {},
      xAxis: {
        data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'],
      },
      yAxis: {},
      series: [
        {
          name: '销量',
          type: 'bar',
          data: [5, 20, 36, 10, 10, 20],
        },
      ],
    })
  },
  methods: {
    draw(legend, xAxis, series) {
      let Echarts1 = this.$echarts.init(document.getElementById('main2'))
      let options = {
        title: {
          text: '绘画量',
        },
        // 鼠标放上去有相应的提示
        tooltip: {
          trigger: 'axis',
        },
        legend: {
          data: legend,
        },
        xAxis: {
          type: 'category',
          data: xAxis,
        },
        yAxis: {
          type: 'value',
        },
        series: series,
      }
      Echarts1.setOption(options)
    },
  },
}
</script>

<style lang="scss">
.data-view {
  width: 100%;
  display: flex;
  justify-content: space-between;
  .el-card {
    width: 50%;
    #main1,
    #main2 {
      width: 100%;
      height: 500px;
    }
  }
}
</style>
